package com.piz.web.order;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.ModelAndView;

import com.piz.model.City;
import com.piz.service.common.AppCache;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.model.Cart;
import com.piz.service.model.PizzaCategoryBean;
import com.piz.service.model.PizzaItem;
import com.piz.service.model.SideCategoryBean;
import com.piz.service.model.SideItem;
import com.piz.service.orderservice.OrderService;
import com.piz.service.productservice.ProductService;

@Controller
public class SelectPizza{
	
	@Autowired
	OrderService orderService;
	
	@Autowired
	ProductService productService;
	
	@RequestMapping(value="selectpizza", method = RequestMethod.GET)
	public ModelAndView viewPizzas(@RequestParam(value="fromSides", defaultValue="false") Boolean fromSides,Model model, HttpServletRequest request){
		
		Map<String, Object> refrenceData = new HashMap<String, Object>();
		List<PizzaCategoryBean> bean = new ArrayList<PizzaCategoryBean>();
		List<SideCategoryBean> sidebean =  new ArrayList<SideCategoryBean>();
		if(fromSides)
		{
			try {
				sidebean = orderService.fetchSideCategoryBean();
			} catch (UnexpectedServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			refrenceData.put("sidebean", sidebean);
			refrenceData.put("quantities", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
		}
		else
		{
			 try {
				bean = orderService.fetchPizzaCategoryBeans();
			} catch (UnexpectedServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 refrenceData.put("bean", bean);
		}
		Cart cart = (Cart) AppCache.getInstance().getCartInfo(RequestContextHolder.currentRequestAttributes().getSessionId());
		if(cart == null)
		{
			cart = new Cart();
			cart.setPizzas(new ArrayList<PizzaItem>());
			AppCache.getInstance().setCartInfo(RequestContextHolder.currentRequestAttributes().getSessionId(), cart);
		}
		refrenceData.put("cart", cart);
		refrenceData.put("fromSides", fromSides);
		refrenceData.put("pizzaConfigAction", "configPizza.action");
		model.addAllAttributes(refrenceData);
		return new ModelAndView("pizza/pizzaBrowser","refrenceData",refrenceData);
	}
	
	@RequestMapping(value="configPizza", method = RequestMethod.GET)
	public ModelAndView configPizza(Model model, HttpServletRequest request, @RequestParam Long pizzaId){
		PizzaItem pizza = null;
		try {
			pizza = orderService.fetchPizzaItemById(pizzaId);
		} catch (UnexpectedServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map<String, Object> refrenceData = new HashMap<String, Object>();
		try {
			refrenceData.put("vegTopps", productService.getVegToppings());
			refrenceData.put("nonVegTopps", productService.getNonVegToppings());
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		
		refrenceData.put("quantities", Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
		model.addAttribute("pizza", pizza);
		model.addAllAttributes(refrenceData);
		return new ModelAndView("pizza/pizzaconfig","refrenceData",refrenceData);
	}
	
	@RequestMapping(value="addAnotherPizza", method = RequestMethod.POST)
	public @ResponseBody boolean addAnotherPizza(@ModelAttribute(value="pizza") PizzaItem pizza, BindingResult result)
	{
		try {
			orderService.addtoCart(pizza);
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	@RequestMapping(value="addSideToCart", method = RequestMethod.POST)
	public @ResponseBody boolean addSideToCart(@ModelAttribute(value="side") SideItem side, BindingResult result)
	{
		try {
			orderService.addSidetoCart(side);
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	@RequestMapping(value = "removeFromCart",headers = "Accept=*/*", method = RequestMethod.GET)
	public @ResponseBody
	boolean removeFromCart(@RequestParam(value = "pizza", required = true) int index, @RequestParam(value="removeSide", defaultValue="false") boolean removeSide,@RequestParam(value="removeDiscount", defaultValue="false") boolean removeDiscount)
	{
		orderService.removeFromCart(index,removeSide,removeDiscount);
		return true;
		
	}
	
	@RequestMapping(value="calculatePizzaPrice",method=RequestMethod.POST)
	public @ResponseBody Double calculateRates(@ModelAttribute(value="pizza") PizzaItem pizza, BindingResult result ){
		
		Double price = null;
		try {
			price = orderService.calculateNetPizzaPrice(pizza);
		} catch (UnexpectedServiceException e) {
			e.printStackTrace();
		}
		return price;
	}
	
	@RequestMapping(value="redeamCouponAction",method=RequestMethod.GET)
	public ModelAndView redeamCoupon(@RequestParam(defaultValue="", required = false) String coupon_code,Model model)
	{
		Long cId = orderService.addDiscountCoupontoCart(coupon_code);
		List<String> errors = new ArrayList<String>();
		if(cId != null)
		{
			return new ModelAndView("redirect:confirmorder.action");
		}
		else
		{
			errors.add("Please Enter a Valid coupon code.");
			model.addAttribute("errors", errors);
			return new ModelAndView("redirect: selectpizza.action?error=true");
		}
	}
	
	@RequestMapping(value = "validateCoupon",headers = "Accept=*/*", method = RequestMethod.GET)
	public @ResponseBody
	Set<String> validateCoupon(@RequestParam(value = "code", defaultValue = "") String code)
	{
		Set<String> errors = new HashSet<String>();
		errors.add(orderService.isCouponValid(code));
		return errors;
	}

}
